package com.shijunhao.graduation.service.business.impl;

import com.shijunhao.graduation.domain.bean.EveryDayInfo;
import com.shijunhao.graduation.domain.dto.BusinessForumDto;
import com.shijunhao.graduation.domain.dto.BusinessForumThymeDto;
import com.shijunhao.graduation.domain.dto.BusinessForumTypeDto;
import com.shijunhao.graduation.domain.entity.BusinessForumThyme;
import com.shijunhao.graduation.domain.entity.BusinessForumType;
import com.shijunhao.graduation.domain.vo.BusinessForumThymeVo;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.BusinessForumThymeMapper;
import com.shijunhao.graduation.service.business.IBusinessForumThymeService;
import com.shijunhao.graduation.service.scheduled.ForumTypeScheduleService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@Service
public class BusinessForumThymeServiceImpl extends BaseServiceImpl<BusinessForumThymeMapper, BusinessForumThyme> implements IBusinessForumThymeService {

    @Resource
    private ForumTypeScheduleService forumTypeScheduleService;

    @Override
    public Long insert(BusinessForumThymeDto dto) {
        BusinessForumThyme entity = new BusinessForumThyme();
        BeanUtils.copyProperties(dto, entity);
        boolean b = this.insertOne(entity);
        if (!b)
            throw new BusinessException("添加失败");
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BusinessForumThymeDto dto) {
        BusinessForumThyme byId = getById(dto.getId());
        BeanUtils.copyProperties(dto,byId);
        return updateById(byId);
    }


    @Override
    public BusinessForumThymeVo toVo(BusinessForumThyme entity) {
        BusinessForumThymeVo result = new BusinessForumThymeVo();
        BeanUtils.copyProperties(entity, result);
        EveryDayInfo info = forumTypeScheduleService.getInfo(entity.getId());
        result.setToday(info);
        return result;
    }
}
